package ngramencoding;

import java.util.*; 	// for file listing
import java.io.*; 	// for file listing

/** ============================================================================
 * Contains auxiliary methods used in the other classes.
 * @author Susan Higashi
 */
public class Utilities 
{
    /** ------------------------------------------------------------------------
     * Returns the Operating System (OS) in which the program is been executed.
     * @return "linux" for Linux OS or "windows" for Windows OS.
     */
    public String getOS()
    {
        String os = System.getProperties().getProperty("os.name");
        
        if(os.startsWith("Linux") || os.startsWith("linux")) os = "linux";
        else if(os.startsWith("Windows") || os.startsWith("windows") )os = "windows";
        else System.out.println("Unkown Operating System!");
        
        return os; 
    }

    /** ------------------------------------------------------------------------
     * Recursively walk a directory tree and return a List of all
     * Files found; the List is sorted using File.compareTo().
     * @author Alex Wong (javapractices.com)
     * @param startingDir is a valid directory, which can be read.
     */
    static public List<File> getFileListing(File startingDir) throws FileNotFoundException 
    {
        validateDirectory(startingDir);
        List<File> result = getFileListingNoSort(startingDir);
        Collections.sort(result);
        return result;
    }

    /** ------------------------------------------------------------------------
    * @param startingDir, directory path in which it is desireable to list its files.
    * @return a list of files.
    * @throws java.io.FileNotFoundException
    */
    static private List<File> getFileListingNoSort(File startingDir) throws FileNotFoundException 
    {
        List<File> result = new ArrayList<File>();
        File[] filesAndDirs = startingDir.listFiles();
        List<File> filesDirs = Arrays.asList(filesAndDirs);
        
        for(File file : filesDirs) 
        {
            result.add(file); //always add, even if directory
            if ( ! file.isFile() ) 
            {
                //must be a directory; recursive call!
                List<File> deeperList = getFileListingNoSort(file);
                result.addAll(deeperList);
            }
        }
        return result;
    }

    /** ------------------------------------------------------------------------
    * Validates a airectory if it exists, does not represent a file, and can be read.
    */
    static private void validateDirectory (File directory) throws FileNotFoundException 
    {
        if (directory == null)          throw new IllegalArgumentException("Directory should not be null.");
        if (!directory.exists())        throw new FileNotFoundException("Directory does not exist: " + directory);
        if (!directory.isDirectory())   throw new IllegalArgumentException("Is not a directory: " + directory);
        if (!directory.canRead())       throw new IllegalArgumentException("Directory cannot be read: " + directory);
    }
}